/******************************************************************************
 *    Copyright 2013 IT2media - IT-Solutions for Directory Business           *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/
package de.it2media.javasitemapgenerator.url.extensions;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map.Entry;

import de.it2media.javasitemapgenerator.header.XMLNS;

public class JavaSitemapGeneratorImageURL extends AbstractURLExtension {

  protected final String IMAGE_LOC          = "loc";
  protected final String IMAGE_CAPTION      = "caption";
  protected final String IMAGE_GEO_LOCATION = "geo_location";
  protected final String IMAGE_TITLE        = "title";
  protected final String IMAGE_LICENSE      = "license";

  /**
   * Here is only one needed parameter<br>
   * LOC = Image Location<br>
   * e.g.: http://www.it2media.com/templates/it2media/images/logo.png<br>
   * <br>
   * 
   * @param imageLoc
   */
  public JavaSitemapGeneratorImageURL(URL imageLoc) {
    content.put("loc", imageLoc);
  }

  /**
   * Here is only one needed parameter<br>
   * LOC = Image Location<br>
   * e.g.: http://www.it2media.com/templates/it2media/images/logo.png<br>
   * <br>
   * 
   * @param loc<br>
   * @throws MalformedURLException
   */
  public JavaSitemapGeneratorImageURL(String loc) throws MalformedURLException {
    this(new URL(loc));
  }

  /**
   * Returns the caption of the image.
   * 
   * @return
   */
  public String getCaption() {
    return (String) content.get(IMAGE_CAPTION);
  }

  /**
   * Sets the caption of the image.
   * 
   * @param caption
   * @return
   */
  public JavaSitemapGeneratorImageURL setCaption(String caption) {
    content.put(IMAGE_CAPTION, caption);
    return this;
  }

  /**
   * Returns the geographic location of the image.
   * For example: Limerick, Ireland
   * 
   * @return
   */
  public String getGeo_location() {
    return (String) content.get(IMAGE_GEO_LOCATION);
  }

  /**
   * Sets the geographic location of the image.
   * For example: Limerick, Ireland
   * 
   * @param geo_location
   * @return
   */
  public JavaSitemapGeneratorImageURL setGeo_location(String geo_location) {
    content.put(IMAGE_GEO_LOCATION, geo_location);
    return this;
  }

  /**
   * Returns the title of the image.
   * 
   * @return
   */
  public String getTitle() {
    return (String) content.get(IMAGE_TITLE);
  }

  /**
   * Sets the title of the image.
   * 
   * @param title
   * @return
   */
  public JavaSitemapGeneratorImageURL setTitle(String title) {
    content.put(IMAGE_TITLE, title);
    return this;
  }

  /**
   * Returns the URL to a license of the image.
   * 
   * @return
   */
  public URL getLicense() {
    return (URL) content.get(IMAGE_LICENSE);
  }

  /**
   * Sets the URL to a license of the image.
   * 
   * @param license
   * @return
   */
  public JavaSitemapGeneratorImageURL setLicense(URL license) {
    content.put(IMAGE_LICENSE, license);
    return this;
  }

  /**
   * Returns the xmlns for the sitemap
   * And of course, its always the same!
   */
  public XMLNS getXmlns() {
    return XMLNS.SITEMAPIMAGE11;
  }

  /**
   * Export this URL to a little proper xml fragment for our image url
   */
  public String toXMLFragment() {

    // we will return the String from the builder
    StringBuilder sb = new StringBuilder("    <image:image>\n");

    for (Entry<String, Object> entry : content.entrySet()) {
      sb.append("      <image:" + entry.getKey() + ">" + String.valueOf(entry.getValue()) + "</image:" + entry.getKey() + ">\n");
    }

    // the url part
    sb.append("    </image:image>\n");

    // return the xml framgent for this image url
    return sb.toString();
  }
}
